package com.jt.tenant.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.FreeWayDetailAcitity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetFreeWayOrder;
import com.jt.tenant.widget.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class FreewayOrderAdapter extends BaseAdapter {

    private Context context;

    private List<GetFreeWayOrder.Item> list = new ArrayList<>();

    public FreewayOrderAdapter(Context context, List<GetFreeWayOrder.Item> list) {

        this.context = context;

        this.list = list;

    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {

        return null;

    }

    @Override
    public long getItemId(int position) {

        return position;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_freeway_order, null);

            JTApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder = new ViewHolder();

            holder.order_id = (TextView) convertView.findViewById(R.id.tv_etc_order_id);

            holder.order_details = (TextView) convertView.findViewById(R.id.tv_etc_order_details);

            holder.order_time = (TextView) convertView.findViewById(R.id.tv_etc_order_time);

            holder.order_meilage = (TextView) convertView.findViewById(R.id.tv_etc_order_meilage);

            holder.order_start_point = (TextView) convertView.findViewById(R.id.tv_etc_order_start_point);

            holder.order_end_point = (TextView) convertView.findViewById(R.id.tv_etc_order_end_point);

            holder.order_station = (TextView) convertView.findViewById(R.id.tv_etc_order_station);

            holder.order_details.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, FreeWayDetailAcitity.class);

                    intent.putExtra("id", list.get(position).id);

                    intent.putExtra("meilage", list.get(position).meilage);

                    if(list.get(position).end_time.equals("")||list.get(position).start_time.equals("")){

                        intent.putExtra("time","");

                        intent.putExtra("flag","0");

                    }else {

                        intent.putExtra("time", (Long.parseLong(list.get(position).end_time) - Long.parseLong(list.get(position).start_time)) / 1000 / 60);

                        intent.putExtra("flag","1");

                    }

                    intent.putExtra("station",list.get(position).station);

                    context.startActivity(intent);

                }
            });

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        holder.order_id.setText("行程编号：" + list.get(position).id);

        holder.order_time.setText(Tools.getTime2(list.get(position).start_time) + " --- " + Tools.getTime2(list.get(position).end_time));

        holder.order_meilage.setText(Tools.F_num(list.get(position).meilage)+"公里");

        holder.order_start_point.setText(list.get(position).start_point);

        holder.order_end_point.setText(list.get(position).end_point);

        holder.order_station.setText("途径" + Tools.F_num(list.get(position).station) + "个收费站，共消费" + Tools.F_num(list.get(position).money) + "元");

        return convertView;

    }

    class ViewHolder {

        private TextView order_id, order_details, order_time, order_meilage, order_start_point, order_end_point, order_station;

    }

}